/*****************************************************************************************
This file is a part of Arise library.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

Arise Engine		Material.cpp

					E-mail:		arise_engine@mail.ru

					Origin by:	DK Engine 0.019,
								dev (avon.dn.ua@gmail.com), Agressor (agress0r@mail.ru)

created:			31.08.2005
last changes:		02.03.2008
*****************************************************************************************/
#include "stdafx.h"


Material::Material()
{
	transparency	= 0;
	m_effect_file	= 0;
}
Material::Material(const MaterialHeader& material_header, TexturePool* texture_pool)
{
	if(!texture_pool) return;

	transparency	= material_header.transparency;
	m_effect_file	= 0;

	for(UINT i = 0; i < 3; i++)
	{
		texture_layers[i].name = material_header.texture_layers[i].c_str();
		
		if(!texture_layers[i].name.empty())
			texture_layers[i].map = texture_pool->add(texture_layers[i].name);
	}

}
Material::~Material()
{
}


void Material::Init()
{
}


void Material::Update()
{
}


void Material::Release()
{
	SAFE_RELEASE(m_effect_file);
}


bool Material::IsEqual(const MaterialHeader& material_header)
{
	for(UINT i = 0; i < 3; i++)
	{
		if(texture_layers[i].name != material_header.texture_layers[i])
			return false;
	}

	if(transparency != material_header.transparency)
		return false;

	return true;
}


void Material::Dump()
{
	UINT color = 6;
	log(color, "---------------------------------------------");
	log(color, "Material");
	log(color, "---------------------------------------------");

	for(int i = 0; i < 3; i++)
	{
		if(texture_layers[i].name.empty())
			log(color, "... texture_layer #%d - '%s'", i, texture_layers[i].name.c_str());
	}

	log(color, "---------------------------------------------");
}

//-------------------------------------------------------------------------------------//

MaterialHeader::MaterialHeader()
{
	queue			= MATERIAL_QUEUE_SOLID;
	material_id		= -1;
	transparency	= 0;
	effect_file		= "shader2.fx";///
}


bool MaterialHeader::LoadFromFile(FileData* file)
{
	if(!file) return false;

	__int16 is_opacity = 0;
	file->Read(is_opacity);
	if(is_opacity) queue = MATERIAL_QUEUE_ALPHATEST;

	file->Read(transparency);
	if(transparency) queue = MATERIAL_QUEUE_TRANSPARENCY;

	texture_layers[0] 	= file->ReadString();	//	diffuse_map
	texture_layers[1] 	= file->ReadString();	//	lighting_map

	return true;
}


void MaterialHeader::Dump(const std::string parent_name)
{
	UINT color = 6;
	log(color, "---------------------------------------------");
	if(!parent_name.empty())
		log(color, "MaterialHeader for %s", parent_name.c_str());
	else
		log(color, "MaterialHeader");
	log(color, "---------------------------------------------");

	for(int i = 0; i < 3; i++)
	{
		log(color, "... queue %d", queue);
		log(color, "... texture layer #%d: %s", i, texture_layers[i].c_str());
	}

	log(color, "---------------------------------------------");
}